//
//  GroupMemeberViewController.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/4/5.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "GroupMemeberViewController.h"
#import "FriendName.h"
#import "GroupMemberTableViewCell.h"
#import "GroupMemberCreatorTableViewCell.h"
#import "MyChatViewController.h"
#import "SearchMyFriendViewController.h"

@interface GroupMemeberViewController ()<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>

@property (nonatomic, strong) UITableView *groupMemberTableView;
@property (nonatomic, strong) UISearchBar *mySearchBar; // 搜索框
@property (nonatomic, strong) NSMutableDictionary *friendNameDic; // 好友姓名字典
@property (nonatomic, strong) NSMutableArray *friendKeyArray; // 好友姓名key


@end

@implementation GroupMemeberViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    // navi的修改
    [self changeNavigation];
   
    // 模拟数据源
    [self addData];
    // 布局视图
    [self addMySubview];
}

// navi的修改
- (void)changeNavigation
{
    [self.navigationController.navigationBar setTranslucent:NO];
    self.navigationItem.title = @"小组成员";
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    leftImageView.image = [UIImage imageNamed:@"houtui"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftImageView];
    UITapGestureRecognizer *leftTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lastPage:)];
    [leftImageView addGestureRecognizer:leftTapGes];
    
}

#pragma mark - 返回上一页面
- (void)lastPage:(UITapGestureRecognizer *)tapGes
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 添加视图
- (void)addMySubview
{
    self.groupMemberTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 44 * kAdjustParamater) style:UITableViewStyleGrouped];
    self.groupMemberTableView.backgroundColor = [UIColor whiteColor];
    self.groupMemberTableView.dataSource = self;
    self.groupMemberTableView.delegate = self;
    [self.view addSubview:_groupMemberTableView];
    
    UIView *whiteBackGround = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 55 * kAdjustParamater)];
    whiteBackGround.backgroundColor = [UIColor whiteColor];
    
    UIView *graySearchview = [[UIView alloc] initWithFrame:CGRectMake(35 / 2 * kAdjustParamater, 15 / 2 * kAdjustParamater, 340 * kAdjustParamater, 30 * kAdjustParamater)];
    graySearchview.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.05];
    graySearchview.layer.cornerRadius = 5 * kAdjustParamater;
    [whiteBackGround addSubview:graySearchview];
    UIImageView *searchImageV = [[UIImageView alloc] initWithFrame:CGRectMake(130 * kAdjustParamater, 5 * kAdjustParamater, 20 * kAdjustParamater, 20 * kAdjustParamater)];
  //  searchImageV.backgroundColor = [UIColor redColor];
    searchImageV.image = [UIImage imageNamed:@"huisekuang-tianjia"];
    [graySearchview addSubview:searchImageV];
    UILabel *searchLabel = [[UILabel alloc] initWithFrame:CGRectMake(searchImageV.right + 10 * kAdjustParamater, 5 * kAdjustParamater, 50 * kAdjustParamater, 20 * kAdjustParamater)];
  //  searchLabel.backgroundColor = [UIColor redColor];
    searchLabel.text = @"搜索";
    searchLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    searchLabel.textAlignment = NSTextAlignmentLeft;
    searchLabel.font = [UIFont systemFontOfSize:k17Big];
    [graySearchview addSubview:searchLabel];
    
    UITapGestureRecognizer *tapGesForSearch = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(search:)];
    [graySearchview addGestureRecognizer:tapGesForSearch];
    self.groupMemberTableView.tableHeaderView = whiteBackGround;
}

- (void)search:(UITapGestureRecognizer *)tapGes
{
    NSLog(@"搜索");
    SearchMyFriendViewController *searchMyFriendVC = [[SearchMyFriendViewController alloc] init];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:searchMyFriendVC];
    [self presentViewController:naVC animated:YES completion:nil];
}

// 模拟数据源
- (void)addData
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Class25ContactList" ofType:@"plist"];
    NSDictionary *tempDic = [NSDictionary dictionaryWithContentsOfFile:path];
    NSArray *nameArray = [tempDic allKeys];
    self.friendNameDic = [NSMutableDictionary dictionary];
    for (int i = 0; i < nameArray.count; i++) {
        NSString *key = nameArray[i];
        NSArray *tempArray = [tempDic valueForKey:key];
        NSMutableArray *array = [NSMutableArray array];
        for (NSDictionary *myDic in tempArray) {
            FriendName *friendModel = [[FriendName alloc] init];
            [friendModel setValuesForKeysWithDictionary:myDic];
            [array addObject:friendModel];
        }
        [self.friendNameDic setValue:array forKey:nameArray[i]];
    }
    NSArray *oneArray = [[self.friendNameDic allKeys] sortedArrayUsingSelector:@selector(compare:)];
    self.friendKeyArray = [NSMutableArray arrayWithArray:oneArray];
    
}


// tableview代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.friendKeyArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *key = self.friendKeyArray[section];
    NSArray *value = self.friendNameDic[key];
    return value.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 取得model
    NSString *key = self.friendKeyArray[indexPath.section];
    NSArray *tempArray = self.friendNameDic[key];
    FriendName *tempModel = tempArray[indexPath.row];
    if (indexPath.section == 0 && indexPath.row == 0) {
     static NSString *identifier = @"creator";
        GroupMemberCreatorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[GroupMemberCreatorTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = tempModel;
        return cell;
    }
    static NSString *identifier = @"friend";
    GroupMemberTableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[GroupMemberTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
     cell.model = tempModel;
    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70 * kAdjustParamater;
}

// 分区头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *grayVIew = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 25 * kAdjustParamater)];
    grayVIew.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.05];

    UILabel *result = [[UILabel alloc] initWithFrame:CGRectMake(20 * kAdjustParamater, 2 * kAdjustParamater, 100 * kAdjustParamater, 20 * kAdjustParamater)];
   // result.backgroundColor = [UIColor redColor];
    NSString *key = self.friendKeyArray[section];
        NSArray *array = self.friendNameDic[key];
    result.text = [NSString stringWithFormat:@"%@(%ld人)", key, array.count];
    [grayVIew addSubview:result];
    return grayVIew;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 25 * kAdjustParamater;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0.1)];
    view.backgroundColor = [UIColor whiteColor];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1 * kAdjustParamater;
}

// 右边的编辑搜索数组
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.friendKeyArray;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    NSString *key = self.friendKeyArray[indexPath.section];
    NSArray *tempArray = self.friendNameDic[key];
    FriendName *tempModel = tempArray[indexPath.row];
    NSLog(@"name = %@", tempModel.name);
    
    MyChatViewController *myChatVC = [[MyChatViewController alloc] init];
    myChatVC.conversationType = ConversationType_PRIVATE;
    myChatVC.title = tempModel.name;
    [self.navigationController pushViewController:myChatVC animated:YES];
}










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
